<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客户" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batch">
        <el-input
          v-model="queryParams.batch"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="xqsaleList"
      show-summary
      :summary-method="getSummaries"
    >
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="客户" align="center" prop="supplyName" />
      <el-table-column label="批次号" align="center" prop="batch" />
      <el-table-column label="销售单号" align="center" prop="saleCode" />
      <el-table-column label="商品码" align="center" prop="productCode" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="数量" align="center" prop="sum" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="总金额" align="center" prop="zprice" />
      <el-table-column label="利润" align="center" prop="profit">
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listXqsaleProfit } from "@/api/supermarket/xqsale";

export default {
  name: "Xqsale",
  data() {
    return {
      // 遮罩层
      loading: true,

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      xqsaleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        saleId: null,
        productId: null,
        sum: null,
        profit: null,
        price: null,
        zprice: null,
        isRk: null,
        batch: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listXqsaleProfit(this.queryParams).then((response) => {
        this.xqsaleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        xqsaleId: null,
        saleId: null,
        productId: null,
        sum: null,
        profit: null,
        price: null,
        zprice: null,
        isRk: null,
        batch: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
       if (index === 1) {
          sums[index] = "";
          return;
        }
         if (index === 4) {
          sums[index] = "";
          return;
        }
         if (index === 6) {
          sums[index] = "";
          return;
        }
         if (index === 7) {
          sums[index] = "";
          return;
        }
         if (index === 8) {
          sums[index] = "";
          return;
        }
       
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
  },
};
</script>
